function can_goods_be_bina() {
	return this.getPerm().can_goods_be_bina;
};
var skill = {
	isGuideUser: function(user_id) {
		var guide_list = this.guide_list || [];
		return guide_list.some(function(guide_id) {
			var guide = BusGuideCon.get(guide_id);
			return guide && guide.user_id == user_id
		});
	},
	/*导购链接，推荐返利版*/
	getGuideConfig: function() {
		var guide_config = this.guide_config || [];
		return guide_config;
	},
	updateGuideConfig: function(new_guide_config) {
		if (new_guide_config instanceof Array) {
			this.guide_config = new_guide_config.map(function(config) {
				config.max_price = parseFloat(config.max_price) || 0;
				config.rate = parseFloat(config.rate) || 0;
				return config;
			});
			this.save();
		} else {
			throwCusError("参数错误")
		}
	},
	getGuideConfigBuff: function() {
		var self = this;
		var buff_id = self.guide_config_buff_id;
		var buff = buff_id && BuffCon.get(buff_id);
		if (!buff) {
			buff = new BuffCon({
				name: "导购链接",
				hidden: true,
				code_id: "导购返利",
				config: {
					bus_id: self._id
				}
			});
			buff_id = self.guide_config_buff_id = buff._id;
			this.save();
		}
		return BuffCon.get(buff_id);
	},
	guideAccToMoney: function(acc) {
		guide_config = this.getGuideConfig();
		for (var i = 0, config; config = guide_config[i]; i += 1) {
			var max_price = parseFloat(config.max_price) || 0;
			console.log(acc, max_price);
			if (acc < max_price) {
				var rate = parseFloat(config.rate) || 0;
				var money = acc * rate;
				break;
			}
		}
		if ((!money) && (config = guide_config[i - 1])) {
			max_price = parseFloat(config.max_price) || 0;
			rate = parseFloat(config.rate) || 0;
			money = acc * rate;
		}
		return money;
	},
	/*导购链接，分销返利版*/
	setGuideBinaBuff: function(bina_buff_info) {
		bina_buff_info = this._formatBinaBuffConfig(bina_buff_info);
		var data_guide_bina_buff = this.getGuideBinaBuff();
		data_guide_bina_buff.setConfig(bina_buff_info);
		return data_guide_bina_buff;
	}.perm(can_goods_be_bina, "权限不足，商家未开启商品分销功能"),
	getGuideBinaBuff: function() {
		var data_guide_bina_buff_id = this.getPerm().data_guide_bina_buff_id;
		console.log("data_guide_bina_buff_id:", data_guide_bina_buff_id);
		if (!data_guide_bina_buff_id) {
			var data_guide_bina_buff = new BuffCon({
				name: "商品分销",
				code_id: "商品分销",
				config: {
					first: 0.2,
					second: 0.1,
					third: 0
				}
			});
			this.setPerm("data_guide_bina_buff_id", data_guide_bina_buff._id);
		} else {
			data_guide_bina_buff = BuffCon.get(data_guide_bina_buff_id);
		}
		return data_guide_bina_buff;
	}.perm(can_goods_be_bina, "权限不足，商家未开启商品分销功能"),
	//员工列表
	getGuideList: function() {
		var guide_list = this.guide_list || [];
		var result = [];
		var new_guide_list = [];
		guide_list.forEach(function(guide_id) {
			var guide = BusGuideCon.get(guide_id);
			if (guide && (guide = guide.getDetail())) {
				new_guide_list.push(guide_id);
				result.push(guide)
			}
		});
		if (new_guide_list.length !== guide_list.length) {
			this.guide_list = new_guide_list;
			this.save();
		}
		return result;
	},
	buildGuideByUserId: function(user_id) {
		var guide_list = this.guide_list || (this.guide_list = []);
		var guide_id = BusGuideCon.build({
			user_id: user_id,
			bus_id: this._id
		});
		if (guide_list.indexOf(guide_id) === -1) {
			guide_list.unshift(guide_id);
			this.save();
		} else {
			throwCusError("该用户已经和商家绑定了导购链接合作协议")
		}
		return guide_id;
	},
	addGuideByUserPhone: function(user_phone) {
		var user = db.find_one(config.dbname.user, {
			info: {
				mobile_phone: user_phone
			}
		});
		if (!user) {
			throwCusError("找不到指定用户")
		}
		var guide = this.buildGuideByUserId(user._id);
		return guide;
	},
	_getGuide: function(guide_id) {
		var guide_list = this.guide_list || [];
		if (guide_list.indexOf(guide_id) === -1) {
			throwCusError("导购不属于指定商家")
		}
		var guide = BusGuideCon.get(guide_id);
		if (!guide) {
			throwCusError("找不到指定导购");
		}
		return guide;
	},
	removeGuide: function(guide_id) {
		var guide = this._getGuide(guide_id);
		guide.destroy();
		this.guide_list = $Array.remove(this.guide_list, guide_id);
		this.save();
	},
	guideAccToMoney: function(acc) {
		guide_config = this.getGuideConfig();
		for (var i = 0, config; config = guide_config[i]; i += 1) {
			var max_price = parseFloat(config.max_price) || 0;
			console.log(acc, max_price);
			if (acc < max_price) {
				var rate = parseFloat(config.rate) || 0;
				var money = acc * rate;
				break;
			}
		}
		if ((!money) && (config = guide_config[i - 1])) {
			max_price = parseFloat(config.max_price) || 0;
			rate = parseFloat(config.rate) || 0;
			money = acc * rate;
		}
		return money;
	},
	state_guide_shenqingduihuanzhong_to_shangjiayijingfukuan: function(user_id) {
		var guide = BusGuideCon.get(BusGuideCon.build({
			bus_id: this._id,
			user_id: user_id
		}));
		if (!guide.state) {
			guide.state = BusGuideCon.STATE.未申请兑换;
		}
		if (guide.state == BusGuideCon.STATE.申请兑换中) {
			guide._nextFlowStatus();
		} else {
			throwCusError("兑现所在状态无法进行相应转换");
		}
		guide.user = UserCon.get(guide.user_id);
		guide.bus = BusCon.get(guide.bus_id);
		return guide;
	},
};
module.exports = skill;
var db = require("../db/index");
var DomainCon = require("../domain/_con");
var GoodsCon = require("../goods/_con");
var UserCon = require("../../core/user/_con");
var BusCon = require("../../core/bus/_con");
var AchCon = require("../ach/_con");
var ClassifyCon = require("../bus.classify/_con");
var TagCon = require("../bus.tag/_con");
var LeagueCon = require("../league/_con");
var BuffCon = require("../buff/_con");
var BusUserBindCon = require("../bus_user_bind/_con");
var BusGuideCon = require("../bus.guide/_con");
var CommentCon = require("../comment/_con");
var config = require("../_include/config");
var data_cut = require("../_include/data_cut");
var helper = require("../_include/helper");
var md5 = require("../_include/md5");
var $Object = require("../_include/$.Object");
var $Array = require("../_include/$.Array");
var tld = require('tldjs');
var fs = require("fs");
var Fiber = require('fibers');
var LBS = require("../_include/baiduLBS");
var midEvent = require("../../middleware/index");